// 创建单例 Singleton
// 立即执行函数是创建闭包的手段之一
let CreateSingleton = (function () {
    console.log('创建单例');
    // 多了一层作用域链 作用域链
    let instance; // 实例
    return function (name) {// constructor
        if (instance) { // 之前实例化过
            return instance;
        }
        this.name = name;
        return instance = this;
        // instance = this;
        // return this;
    }
})()

 CreateSingleton.prototype.getName = function() {
      console.log(this.name);
}

//惰性
let singleton = new CreateSingleton('计涛');
let dalao = new CreateSingleton('大佬');
console.log(singleton === dalao);
console.log(singleton.getName());// 计涛
console.log(dalao.getName());// 计涛

